<?php

namespace app\admin\model;

use think\Model;
use think\Cache;

use \app\admin\model\AuthGroup;
use \app\admin\model\AuthGroupAccess;

class AdminUsers extends Model
{
  // 设置表名
  protected $table = 'a_admin_users';
  protected $pk    = 'id';
  protected $autoWriteTimestamp = true;

  private $zjdata;   // 数据中间层

  public function usersGroup()
  {
    return $this->hasOne('AuthGroupAccess','uid','id')->field('id,uid,group_id');
  }

  /**
  * 取得管理者信息
  *  return Array()
  */
  public function getAllUser(){

    $data =  $this->field('id,name,phone,remark,register_time,last_login_ip,last_login_time,login_count,email,status')->select();
    foreach ($data as $k => $v) {
      $dbData                  =  $this->getUserGroup($v['id']);
      $data[$k]['groupTitle']  =  $dbData['title'];
      $data[$k]['is_admin']    =  $dbData['is_admin'];

      unset($dbData);
      unset($group_id);
    }
    return $data;
  }

  /**
  * 检测用户是否存在
  *  return Array()
  */
  public function checkUserInfo($data){
    $this->zjdata  = $data;
    unset($data);
    $result = $this->where(['phone|email' => $this->zjdata['key'], 'password' => $this->zjdata['password'],'is_delete' => 0])
               ->field('id,name,phone,remark,register_time,last_login_ip,last_login_time,login_count,email,status')
               ->find();
    unset($this->zjdata);
    return $result;
  }

  /**
  * 检测用户是否已注册
  *  return Boolean
  */
  public function checkIsUser($data){
    $result = $this->where(['phone|email' => $data['key']])->count();
    return $result > 0 ? 1 : 0;
  }

  /**
  * 更新登录信息
  *  return Boolean
  */
  public function updateLogin($userId,$login_ip){
    return $this->where('id', $userId)
               ->update([
               'last_login_time' => time(),
               'login_count'     => ['exp','login_count+1'],
               'last_login_ip'   => $login_ip,
              ]);
 }

 /**
 * 创建管理者
 *  return Boolean
 */
 public function createUser($data){
    if ($this->data($data)->allowField(true)->save()) {
       return AuthGroupAccess::insert(['uid'=>$this->id,'group_id'=>$data['group_id']]);
    }
    return false;
 }

 /**
 * 修改管理者信息
 *  return Boolean
 */
 public function editUserInfo($uid,$data){
   if(isset($data['group_id']))
          $object = new AuthGroupAccess;
          $object->save(['group_id'=>$data['group_id']],['uid'=>$uid]);
   return $this->allowField(true)->save($data,['id'=>$uid]);
 }

 /**
 * 取得对应用户组信息
 *  return Array()
 */
 public  function getUserGroup($uid){
   $db        = $this->find($uid)->usersGroup;
   return     AuthGroup::where('id',$db['group_id'])->field('title,is_admin')->find();
 }

 /**
 * 取得对应可操作权限信息
 *  return Array()
 */
 public  function getUserGroupRule($uid){
   $db        = $this->find($uid)->usersGroup;
   return     AuthGroup::where('id',$db['group_id'])->field('rules')->find();
 }

 /**
 * 取得指定ID用户信息
 *  return Array()
 */
 public  function getUserInfo($uid,$type=1){
   if($type != 1){
     return  $this->where('id',$uid)->field('password')->find();
   }
    $group  = $this->find($uid)->usersGroup;
    $data = $this->where('id',$uid)->field('id,name,phone,email,remark,is_veriyk_mail,status')->find();
    $data['group_id'] = $group['group_id'];
    unset($group);
    return $data;
 }

 /**
 * 删除管理者信息 -- 同时删除 角色表 对应内容
 *  return Boolean
 */
 public function delUser($uid){
   $ru = $this->destroy($uid);
   if($ru){
     return AuthGroupAccess::destroy(['uid'=>$uid]);
   }
   return false;
 }

  /*
    批量删除
  */
  public function deleteAll($id)
  {
    return $this->destroy($id);
  }

}
